package koala.kuangjiademo.data;

import android.support.annotation.Nullable;

import java.io.Serializable;
import java.util.List;

public class Attendance implements Serializable {
    int id;
    String attendtype;
    String attendarea;
    String attendaddress;
    Long attendtime;
    String attendsource = "MOBILE";
    String attendcenterid;
    String attendparam;
    String remarks;
    private static final String TYPE_CHECK_IN = "IN";
    private static final String TYPE_CHECK_OUT = "OUT";

    public Attendance type(boolean checkIn) {
        attendtype = checkIn ? TYPE_CHECK_IN : TYPE_CHECK_OUT;
        return this;
    }

    public Attendance address(String address) {
        attendaddress = address;
        return this;
    }

    public Attendance remarks(String remarks) {
        this.remarks = remarks;
        return this;
    }

    public String getAddress() {
        return attendaddress;
    }

    public long getTime() {
        return attendtime != null ? attendtime : System.currentTimeMillis();
    }

    public static class Response extends ResponsePacket<Attendance> {
    }

    public static class Record implements Serializable {
        List<Attendance> records;

        @Nullable
        public Attendance getCheckInRecord() {
            Attendance checIn = null;
            if (records != null) {
                for (int i = 0; i < records.size(); i++) {
                    Attendance a = records.get(i);
                    if (a.attendtype.equals(TYPE_CHECK_IN)) {
                        if (checIn == null || a.attendtime < checIn.attendtime) {
                            checIn = a;
                        }
                    }
                }
            }
            return checIn;
        }

        @Nullable
        public Attendance getCheckOutRecord() {
            Attendance checIn = null;
            if (records != null) {
                for (int i = 0; i < records.size(); i++) {
                    Attendance a = records.get(i);
                    if (a.attendtype.equals(TYPE_CHECK_OUT)) {
                        if (checIn == null || a.attendtime > checIn.attendtime) {
                            checIn = a;
                        }
                    }
                }
            }
            return checIn;
        }
    }

    public static class SearchResponse extends ResponsePacket<Record> {

    }


}
